package com.yunxi.service.urm.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yunxi.core.base.Constant;
import com.yunxi.core.util.BeanTools;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.eai.pojo.esb.EsbVO;
import com.yunxi.eai.pojo.urm.ClueBistributeVO;
import com.yunxi.eai.pojo.urm.UrmUserInfoVO;
import com.yunxi.eai.service.urm.UrmService;
import com.yunxi.hubToBaseData.pojo.CustomerVOURM;
import com.yunxi.hubToBaseData.service.URM2baseDataDubboService;
import com.yunxi.hubToBiz.pojo.CustomerClueVO;
import com.yunxi.hubToBiz.service.NewCustomerClueService;
import com.yunxi.model.eai.EaiLog;
import com.yunxi.rocketmq.producer.EaiLogsProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2019/8/18.
 */
@Service
@Qualifier("urmServiceImpl")
public class UrmServiceImpl implements UrmService {

    @Autowired
    private EaiLogsProducer eaiLogsProducer;
    @Autowired
    private  URM2baseDataDubboService urm2baseDataDubboServiceImpl;
    @Autowired
    private NewCustomerClueService newCustomerClueServiceImpl;
    protected static final Logger logger = LoggerFactory.getLogger(UrmServiceImpl.class);


    @Override
    public ApiResult distributeUrm(EsbVO esbVO) {
        String serviceName = esbVO.getServiceName();
        String reqBody = esbVO.getReqBody();
        ApiResult apiResult = null;
        if (serviceName.equals(Constant.URM_SAVE_CLIENT_INFO)) {
            //用户信息同步
            apiResult = saveClientInfo(esbVO);
        }else if (serviceName.equals(Constant.URM_CLUE_BISTRIBUTE)){
            //线索分发
            apiResult = saveClueBistribute(esbVO);
        }else {
            return ResponseUtil.fail(500, "没有对应的业务接口!");
        }
        return apiResult;
    }

    private ApiResult saveClueBistribute(EsbVO esbVO) {
        ApiResult apiResult =ResponseUtil.ok();
        EaiLog eaiLog = new EaiLog();
        eaiLog.setEsbEaiContent(esbVO.getReqBody());
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("URM线索分发给PMS");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        List<ClueBistributeVO> clueBistributeVO = JSON.parseObject(esbVO.getReqBody(), new TypeReference<ArrayList<ClueBistributeVO>>() {
        });
        try {
            //内部业务
            CustomerClueVO customerClueVO=new CustomerClueVO();
            BeanUtils.copyProperties(clueBistributeVO,customerClueVO);
            List<CustomerClueVO> customerClueVOS = BeanTools.copyListToList(clueBistributeVO, CustomerClueVO.class);
            apiResult = newCustomerClueServiceImpl.newCustomerClue(customerClueVOS);
           /* if (apiResult.getCode() == -1) {
                eaiLog.setDisposeStatus(2);
            }*/
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
        } catch (Exception e) {
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
            logger.error(e.getMessage(), e);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return apiResult;
    }

    private ApiResult saveClientInfo(EsbVO esbVO) {
        ApiResult apiResult =ResponseUtil.ok();
        EaiLog eaiLog = new EaiLog();
        eaiLog.setEsbEaiContent(esbVO.getReqBody());
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("URM客户信息同步给PMS");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        JSONObject jsonObject = JSON.parseObject(esbVO.getReqBody());
        List<UrmUserInfoVO> urmUserInfoVOS =JSON.parseObject(String.valueOf( jsonObject.get("data")), new TypeReference<ArrayList<UrmUserInfoVO>>() {});
        try {
            //内部业务
            if (urmUserInfoVOS!=null || urmUserInfoVOS.size()>0 ){
                List<CustomerVOURM> customerVOURMS = BeanTools.copyListToList(urmUserInfoVOS, CustomerVOURM.class);
                apiResult =urm2baseDataDubboServiceImpl.sycnCustomerInfoFromUrm(customerVOURMS);
                if (apiResult.getCode() == -1) {
                    eaiLog.setDisposeStatus(2);
                }
                eaiLog.setResponseReceiveTime(new Date());
                eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
            }

        } catch (Exception e) {
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
            logger.error(e.getMessage(), e);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return apiResult;
    }
}
